package;

import flixel.FlxG;
import flixel.FlxSprite;
import flixel.FlxState;
import flixel.group.FlxTypedGroup;
import flixel.text.FlxText;
import flixel.ui.FlxButton;
import flixel.util.FlxColor;
import flixel.util.FlxPoint;
import flixel.tweens.FlxEase;
import flixel.tweens.FlxEase.EaseFunction;
import flixel.tweens.FlxTween;
import flixel.tweens.FlxTween.TweenOptions;


/**
 * A FlxState which can be used for the actual gameplay.
 */
class PlayState extends FlxState
{
	private var _convo:Conversation;
	private var _secondConvo:Conversation;
	private var _winConvo:Conversation;
	private var _background:FlxSprite;
	private var _backgroundBorder:FlxSprite;
	private var _backgroundStump:FlxSprite;
    private var _treeSegmentSprites:FlxTypedGroup<FlxSprite>;
	private var _treeTopSprite:FlxSprite;
    private var _lastSegY:Float;
	private var _dragMouseStart:Float;
	private var _drag:Bool;
	private var _tween:FlxTween;
	private var _itemLayer:ItemLayer;
	private var _screenHeightDrag:Int = 100;
	private var _winGame:Bool = false;
	private var _secondConvoCount:Int = 0;
	
	/**
	 * Function that is called up when to state is created to set it up. 
	 */
	override public function create():Void
	{
		_background = new FlxSprite(0, -1500, AssetPaths.SkyScaled__png);
		_background.setGraphicSize(576, 0);
		_background.updateHitbox();
		_backgroundBorder = new FlxSprite(0, -1500, AssetPaths.Border__png);
		_backgroundBorder.setGraphicSize(576, 0);
		_backgroundBorder.updateHitbox();
		add(_backgroundBorder);
		add(_background);
		_backgroundStump = new FlxSprite(0, -1500, AssetPaths.StumpScaled__png);
		_backgroundStump.setGraphicSize(576, 0);
		_backgroundStump.updateHitbox();
		add(_backgroundStump);
		_treeTopSprite = new FlxSprite(85, 857, AssetPaths.treerings__png);
		_treeTopSprite.setGraphicSize(400, 0);
		_treeTopSprite.updateHitbox();
		add(_treeTopSprite);
        _treeSegmentSprites = new FlxTypedGroup<FlxSprite>();
        add(_treeSegmentSprites);
        _lastSegY = 912;
		_itemLayer = new ItemLayer();
		add(_itemLayer);
		_convo = new Conversation(AssetPaths.IntroConvo__xml);
		add(_convo);
		//_secondConvo = new Conversation(AssetPaths.Intermission__xml);
		//_secondConvo.set_active(false);
		//_secondConvo.set_visible(false);
		//add(_secondConvo);
		//_winConvo = new Conversation(AssetPaths.GameOver__xml);
		//_winConvo.set_active(false);
		//_winConvo.set_visible(false);
		//add(_winConvo);
		super.create();
		FlxG.camera.scroll.y += 200;
	}
	
	/**
	 * Function that is called when this state is destroyed - you might want to 
	 * consider setting all objects this state uses to null to help garbage collection.
	 */
	override public function destroy():Void
	{
		super.destroy();
	}

	/**
	 * Function that is called once every frame.
	 */
	override public function update():Void
	{
		if (_convo.active)
		{
			_convo.update();
			return;
		}
		if (_secondConvo != null) {
			if (_secondConvo.active) {
				_secondConvo.update();
				return;
			}
		}
		if (_winGame) {
			if (_winConvo.active) {
				_winConvo.update();
			}
			return;
		}
		#if !FLX_NO_MOUSE
		if (FlxG.mouse.justPressed && !_itemLayer.usingMouse())
		{
			_dragMouseStart = FlxG.mouse.screenY;
			_drag = true;
		}
		else if (FlxG.mouse.pressed && _drag)
		{
			FlxG.camera.scroll.y = Math.min(Math.max(FlxG.camera.scroll.y - (FlxG.mouse.screenY - _dragMouseStart), _screenHeightDrag), 200);
			_dragMouseStart = FlxG.mouse.screenY;
		}
		else if (FlxG.mouse.pressed && _itemLayer.scrollScreen())
		{
			if (FlxG.mouse.getScreenPosition().y < 80)
			{
				FlxG.camera.scroll.y -= Math.sqrt(Math.min(80 - FlxG.mouse.getScreenPosition().y, 80));
			}
			else if (FlxG.mouse.getScreenPosition().y > 820)
			{
				FlxG.camera.scroll.y += Math.sqrt(Math.min(FlxG.mouse.getScreenPosition().y - 820, 80));
			}
		}
		else if (FlxG.mouse.justReleased)
		{
			_drag = false;
		}
		#end
		#if !FLX_NO_TOUCH
		var didSelect:Bool = false;
		for (touch in FlxG.touches.justReleased())
		{
			// touch handling stuff
		}
		#end
		for (swipe in FlxG.swipes) 
		{
            // swipe handling stuff
		}
        // Check if the item layer has any events to trigger
        if (_itemLayer.spawnTreeSegment())
        {
            var tempSprite = new FlxSprite();
			var name:String = _itemLayer.newSegmentGraphic();
            tempSprite.loadGraphic(name);
			tempSprite.setGraphicSize(511, 0);
			tempSprite.updateHitbox();
            tempSprite.y = _lastSegY - tempSprite.height + 80;
			_treeTopSprite.y = _lastSegY - tempSprite.height + 24;
			tempSprite.x = 30;
            _lastSegY = tempSprite.y;
            _treeSegmentSprites.add(tempSprite);
			_screenHeightDrag -= Std.int(tempSprite.height);
			if (name.substring(14, name.indexOf(".", 14)) == "RocketPiece") {
				_winGame = true;
				_treeTopSprite.visible = false;
				_winConvo = new Conversation(AssetPaths.GameOver__xml);
				add(_winConvo);
			}
			if (name.substring(14, name.indexOf(".", 14)) == "MetalPiece" && _secondConvoCount == 0) {
				_secondConvo = new Conversation(AssetPaths.Intermission__xml);
				add(_secondConvo);
				_secondConvoCount += 1;
			}
        }
        // if (itemlayer.spawnNewConvo)
        //    _convo = new Conversation(itemlayer.newConvoName);
		super.update();
	}

}